package com.app.net.manager.pay;


import com.app.net.common.AbstractBaseManager;
import com.app.net.common.RequestResultListener;
import com.app.net.req.pay.PayDataReq;
import com.app.net.res.ResultObject;
import com.app.net.res.pay.PayWXRes;
import com.app.utiles.other.Constant;
import com.retrofits.net.common.RequestBack;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;


/**
 * 微信预支付
 * 1:MD5 和 开放平台上的要一致，所以要设置打包
 * 2:WXPayEntryActivity（文件路径为applicationId/wxapi） 必须继承 WXResultActivity
 * 3:WXPayEntryActivity的注册要加上  android:exported="true"
 * Created by Administrator on 2016/9/7.
 * https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12&index=2
 */
public class PayWXManager extends AbstractBaseManager {
    private PayDataReq req;

    public PayWXManager(RequestBack requestBack) {
        super(requestBack);

    }

    @Override
    protected void init() {
        req = new PayDataReq();

        req.pay_provider = "wechatpayapp";
    }

    /***
     * @param out_trade_no 订单id
     * @param amount 支付金额（分）
     * @param subject 支付订单标题
     * @param body 支付订单描述
     */
    public void setData(String out_trade_no, int amount, String subject, String body) {
        req.out_trade_no = out_trade_no;
        req.amount = amount;
        req.subject = subject;
        req.body = body;
    }

    @Override
    protected void request(Retrofit retrofit, String other) {
        ApiPay service = retrofit.create(ApiPay.class);
        HashMap map = getHeadMap();

        Call<ResultObject<PayWXRes>> call = service.payWX(map, req);
        call.enqueue(new RequestResultListener<ResultObject<PayWXRes>>(this, req) {
            @Override
            public Object getObject(Response<ResultObject<PayWXRes>> response) {
                ResultObject<PayWXRes> body = response.body();
                PayWXRes obj = body.obj;
                return obj;
            }
        });
    }


}
